<?php
class House extends CI_Controller
{
    private $a;
    public function __construct() {
        parent::__construct();
    }
    public function getA()
    {
        return $this->a = 1;
    }
}
/*
 * home | view all
 */
class Home extends House
{
    public function  __construct()
    {
        parent::__construct();
        $prefs = array (
               'start_day'    => 'sunday',
               'month_type'   => 'long',
               'day_type'     => 'short',
               'show_next_prev' => TRUE,
               'next_prev_url'   => 'http://localhost/vitbay.vn'
             );

        $this->load->helper("url");
        $this->load->helper("cookie");
        $this->load->library("session");
        $this->load->library("calendar", $prefs);
    }
    /*
     * Kiem tra username and password co ton tai hay khong
     * Neu ton tai username and password se cho dang nhap voi 2 truong hop
     * * TH1: Neu user checkbox remember me
     * * * Thiet lap cookie cho username and password va tu dong sinh ra mot chuoi bat ki(token)
     * * * Luu chuoi token do vao db server dong thoi set_cookie(token)
     * * TH2: Neu user don't checkbox remember me
     * * * just set session for username and password
     *
     * function login
     */
    public function login()
    {
        $u = new User();
        if(isset($_POST['sub']))
        {
            $user = $this->input->post("user");
            $pword = $this->input->post("pword");
            // neu nhap dung username and password
            $u->where('username', $user);
            $u->where('password', $pword)->get();
            $flag = false;
            if($u->result_count()==1)
            {
                // co trang thai
                $flag = true;
            }
            // neu ton tai user
            if($flag==true)
            {
                // neu checkbox remember me
                if($this->input->post("remember"))
                {
                    // tao ra mot token ngau nhien
                    $token = md5(rand(1, 10));
                    // cap nhat no vao db
                    $u->where("username", $user);
                    $u->update("token", $token);
                    // set cookie
                    $username = array('name'=>"user", 'value'=>$user, 'expire'=>"3600");
                    $password = array('name'=>"pword", 'value'=>$pword, 'expire'=>"3600");
                    $token_server = array('name'=>"token", 'value'=>$token, 'expire'=>"3600");

                    set_cookie($username);
                    set_cookie($password);
                    set_cookie($token_server);
                }
                else// khong checkbox remember me
                {
                    $ses = array('user'=>$user);
                    $this->session->set_userdata($ses);
                }

                redirect("home/index");
            }
            else// neu khong ton tai user
            {
                $abc = 1;
                $mas = array('loi'=>"Username or Password invalid.");
            }
        }// End $_POST
        
        if(isset($abc) && $abc==1)
        {
            $this->load->view("backend/login", $mas);
        }
        else
        {
            $this->load->view("backend/login");
        }
    }
    /* neu du lieu tu cookie user gui len server trung voi du lieu tren server
     * thi tu dong login
     * login_successful
     */
    public function login_successful($user, $pword, $token)
    {
        $u = new User();
        $u->get();
        $flag = false;
        foreach($u->all as $ur)
        {
            if($ur->username==$user && $ur->password==$pword && $ur->token==$token)
            {
                $flag = true;
            }
        }

        return $flag;
    }// End login_success

    // index
    public function index()
    {
        if($this->login_successful(get_cookie('user'), get_cookie('pword'), get_cookie('token')) || $this->session->userdata("user"))
        {
            $this->load->view("backend/index");
        }
        else
        {
            redirect("home/login");
        }

    }// End index()

    /*
     ************************ Quan ly nhap hang ****************************
     */
    // ql_nhap()
    public function ql_nhap()
    {
        if(get_cookie('user'))
        {
            $this->load->view("backend/sach/index");
        }
        else
        {
            $this->login();
        }
    }

    // add_product
    public function add_product()
    {
        $s = new Sach();
        if(isset($_POST['sub']))
        {
            $tensach = $this->input->post("name_p");
            $sluong = $this->input->post("sluong_p");
            $s->tensach = $tensach;
            $s->soluong = $sluong;
            if(empty($s->error))
            {
                $s->save();
            }
        }
        $this->load->view("backend/add/add_any");
    }
    /*
     * ******************** End Quan ly nhap hang *************************
     */


    //tinhoc()
    public function view_book()
    {
        $s = new Sach();
        $book = $this->uri->segment(4);
        $s->where("loaisach", $book)->get();
        $id = array(); $ten = array(); $soluong = array(); $gia = array(); $giaban = array(); $tacgia = array();
        $picture = array(); $mota = array(); $nxb = array(); $namxb = array(); $loaisach = array(); $ngaynhap = array();
        foreach($s->all as $ts)
        {
            $id[] = $ts->id;
            $ten[] = $ts->tensach;
            $soluong[] = $ts->soluong;
            $gia[] = $ts->gia;
            $giaban[] = $ts->giaban;
            $tacgia[] = $ts->tacgia;
            $picture[] = $ts->picture;
            $nxb[] = $ts->nxb;
            $namxb[] = $ts->namxb;
            $loaisach[] = $ts->loaisach;
            $ngaynhap[] = $ts->ngaynhap;
            $mota[] = substr($ts->mota, 0, 200);
        }
        $abc = array(8 => 'http://www.tzwebz.zymichost.com');
        $data = array('id'=>$id, 'ten'=>$ten, 'soluong'=>$soluong, 'gia'=>$gia, 'giaban'=>$giaban, 'tacgia'=>$tacgia,
                    'picture'=>$picture,'nxb'=>$nxb, 'namxb'=>$namxb, 'loaisach'=>$loaisach, 'ngaynhap'=>$ngaynhap,
                    'mota'=>$mota, 'calendar'=>$this->calendar->generate(2012, 11, $abc));
        $url = "backend/sach/info";
        $this->load->view($url, $data);
    }
    //
    public function view_all_info()
    {
        $s = new Sach();
        $id = $this->uri->segment(3);
        $s->where("id", $id)->get();
        // get_info_sach_by_id
        foreach($s->all as $ts)
        {
            $data = array('id'=>$ts->id, 'ten'=>$ts->tensach, 'soluong'=>$ts->soluong, 'gia'=>$ts->gia, 'giaban'=>$ts->giaban,
                'tacgia'=>$ts->tacgia, 'picture'=>$ts->picture,'nxb'=>$ts->nxb, 'namxb'=>$ts->namxb, 'loaisach'=>$ts->loaisach, 
                'danhcho'=>$ts->danhcho, 'ngaynhap'=>$ts->ngaynhap, 'mota'=>$ts->mota);
        }
        
        if($_POST)
        {
            if(isset($_POST['update']))
            {
                $flag = 1;
            }
            if(isset($_POST['del']))
            {
                $flag = 2;
            }
            $data['flag'] = $flag;
        }
        $this->load->view("backend/sach/viewone", $data);
    }// End view_all_info()

    // confirm info
    public function confirm_info()
    {
        $id = $this->uri->segment(3);
        $tensach = $this->input->post("tensach");
        $soluong = $this->input->post("soluong");
        $gia = $this->input->post("gia");
        $giaban = $this->input->post("giaban");
        $tacgia = $this->input->post("tacgia");
        $picture = $this->input->post("picture");
        $nxb = $this->input->post("nxb");
        $namxb = $this->input->post("namxb");
        $loaisach = $this->input->post("loaisach");
        $danhcho = $this->input->post("danhcho");
        $ngaynhap = $this->input->post("ngaynhap");
        $mota = $this->input->post("mota");
        switch ($this->input->post("submit"))
        {
            case "Sửa":  $message = "Nhấn Ok để Sửa thông tin cho sản phẩm sách <b style='font-size: 25px; color: blue;'>".$tensach."</b>";
                         $status = 1;
                break;
            case "Xóa":  $message = "Nhấn Ok để Xóa sản phẩm sách <b style='font-size: 25px; color: blue;'>".$tensach."</b>";
                         $status = 2;
                break;
        }
        $data = array('id'=>$id, 'ten'=>$tensach, 'soluong'=>$soluong, 'gia'=>$gia, 'giaban'=>$giaban,
                            'tacgia'=>$tacgia, 'picture'=>$picture, 'nxb'=>$nxb, 'namxb'=>$namxb, 'loaisach'=>$loaisach,
                            'danhcho'=>$danhcho, 'ngaynhap'=>$ngaynhap, 'mota'=>$mota, 'mes'=>$message, 'status'=>$status);
        $this->load->view("backend/sach/confirm_info", $data);
    }// End confirm_info
    //#1F67B3, 00C13F
    // Action
    public function Action()
    {
        $s = new Sach();
        $id = $this->uri->segment(3);
        $stt = $this->uri->segment(4);
        $tensach = $this->input->post("tensach");
        $soluong = $this->input->post("soluong");
        $gia = $this->input->post("gia");
        $giaban = $this->input->post("giaban");
        $tacgia = $this->input->post("tacgia");
        $picture = $this->input->post("picture");
        $nxb = $this->input->post("nxb");
        $namxb = $this->input->post("namxb");
        $loaisach = $this->input->post("loaisach");
        $danhcho = $this->input->post("danhcho");
        $ngaynhap = $this->input->post("ngaynhap");
        $mota = $this->input->post("mota");
        $data = array('tensach'=>$tensach, 'soluong'=>$soluong, 'gia'=>$gia, 'giaban'=>$giaban,
                            'tacgia'=>$tacgia, 'picture'=>$picture, 'nxb'=>$nxb, 'namxb'=>$namxb, 'loaisach'=>$loaisach,
                            'danhcho'=>$danhcho, 'ngaynhap'=>$ngaynhap, 'mota'=>$mota);
        if($this->input->post("ok"))
        {
            $er = array();
            switch ($stt)
            {
                case 1: $s->where('id', $id);
                        if($s->update($data))
                        {
                            $er['mes'] = "Cập nhật thông tin sản phẩm thành công !";
                        }
                        else 
                        {
                            $er['mes'] = "Cập nhật thông tin sản phẩm không thành công !";
                        }
                    break;
                case 2: $s->where('id', $id)->get();
                        if($s->delete())
                        {
                            $er['mes'] = "Bạn đã Xóa thành công !";
                        }  
                        else
                        {
                            $er['mes'] = "Xóa không thành công !";
                        }
                    break;
            }
            $er['loaisach'] = $loaisach;
        }
        $this->load->view("backend/sach/Action", $er);
    }


    // logout
    public function logout()
    {
        delete_cookie("user");
        delete_cookie("pword");
        delete_cookie("token");
        $this->session->sess_destroy();
        $this->login();
    }

    //emxample
    public function add_user()
    {
        $u = new User();
        $u->username = "";
        $u->password = "";
        if($u->save())
        {
            echo "Thanh cong !";
        }
        else
        {
            if(isset($u->error->username))
            {
                echo str_replace($u->error->username, "nhap username", $u->error->username)."<br/>";
            }
            echo "Khong thanh cong !";
            foreach($u->error->all as $e)
            {
                echo $e."<br/>";
            }
        }
    }
}
